#region Copyrights Notice
//©2010 Microsoft Corporation. All rights reserved.
#endregion

#region usings
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
#endregion

namespace Microsoft.InformationSecurity.CISF.SecurityPortal.MasterUI.PortalControls
{
    public partial class SideMenuControl : System.Web.UI.UserControl
    {
        public string _SecondLevelMenuItem;
        public string _AppHomeURL;

        private class MenuItem
        {
            string _Title;
            string _Desc;
            string _Url;

            public MenuItem()
            {
            }
            public MenuItem(string title, string desc, string Url)
            {
                _Title = title.PadRight(75, ' ').PadLeft(150, ' ');
                _Desc = desc;
                _Url = Url;
            }

            public string Title
            {
                get
                {
                    return _Title;
                }
                set
                {
                    _Title = value;
                }
            }

            public string Desc
            {
                get
                {
                    return _Desc;
                }
                set
                {
                    _Desc = value;
                }
            }

            public string Url
            {
                get
                {
                    return _Url;
                }
                set
                {
                    _Url = value;
                }
            }

        }

        private List<MenuItem> ThirdLevelMenuItems
        {
            get
            {
                List<MenuItem> _ThirdLevelMenuItems = Cache["ThirdLevelMenuItems"] as List<MenuItem>;
                if (null == _ThirdLevelMenuItems)
                {
                    _ThirdLevelMenuItems = new List<MenuItem>();

                    Cache["ThirdLevelMenuItems"] = _ThirdLevelMenuItems;
                }

                return _ThirdLevelMenuItems;
            }

        }
        private List<MenuItem> FourthLevelMenuItems
        {
            get
            {
                List<MenuItem> _FourthLevelMenuItems = Cache["FourthLevelMenuItems"] as List<MenuItem>;
                if (null == _FourthLevelMenuItems)
                {
                    _FourthLevelMenuItems = new List<MenuItem>();

                    Cache["FourthLevelMenuItems"] = _FourthLevelMenuItems;
                }

                return _FourthLevelMenuItems;
            }

        }
        protected void Page_Init(object sender, EventArgs e)
        {
            if (ConfigurationManager.AppSettings["ApplicationName"] != null)
                SecondLevelMenuItem = ConfigurationManager.AppSettings["ApplicationName"].ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ThirdLevelMenuItems.Clear();

                //root
                SiteMapNode root = SiteMap.RootNode;

                if (SiteMap.RootNode.HasChildNodes)
                {
                    IEnumerator rootNodesChildrenEnumerator = SiteMap.RootNode.ChildNodes.GetEnumerator();
                    while (rootNodesChildrenEnumerator.MoveNext())
                    {
                        SiteMapNode current = (SiteMapNode)rootNodesChildrenEnumerator.Current;

                        //Second level menu items...
                        if (((SiteMapNode)rootNodesChildrenEnumerator.Current).HasChildNodes)
                        {
                            IEnumerator childNodeChildrenEnumerator = ((SiteMapNode)(rootNodesChildrenEnumerator.Current)).ChildNodes.GetEnumerator();
                            while (childNodeChildrenEnumerator.MoveNext())
                            {
                                SiteMapNode childCurrent = (SiteMapNode)childNodeChildrenEnumerator.Current;

                                if (childCurrent.ToString() == SecondLevelMenuItem) //This is where App Menu starts, applications like "Risk Tracker"
                                {
                                    //first SET the Application Home Text/Link
                                    _AppHomeURL = childCurrent.Url;

                                    //Third level menu items...
                                    if (((SiteMapNode)childNodeChildrenEnumerator.Current).HasChildNodes)
                                    {
                                        IEnumerator ThirdLevelchildNodeChildrenEnumerator = ((SiteMapNode)(childNodeChildrenEnumerator.Current)).ChildNodes.GetEnumerator();
                                        while (ThirdLevelchildNodeChildrenEnumerator.MoveNext())
                                        {
                                            SiteMapNode ThirdLevelchildCurrent = (SiteMapNode)ThirdLevelchildNodeChildrenEnumerator.Current;

                                            bool blnAllow = false;
                                            foreach (string roleName in ThirdLevelchildCurrent.Roles)
                                            {
                                                if (roleName.Equals("*"))
                                                {
                                                    blnAllow = true;
                                                    break;
                                                }
                                                else if (this.Page.User.IsInRole(roleName))
                                                {
                                                    blnAllow = true;
                                                    break;
                                                }
                                            }
                                            if (blnAllow)
                                            {
                                                string Title = ThirdLevelchildCurrent.ToString(); //Title
                                                string Desc = ThirdLevelchildCurrent.Description; //Desc
                                                string Url = ThirdLevelchildCurrent.Url; //Url

                                                MenuItem mi = new MenuItem(Title, Desc, Url);
                                                ThirdLevelMenuItems.Add(mi);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                //remove the side navigation if there are no third level menu items to display
                if (ThirdLevelMenuItems.Count == 0)
                {
                    ThirdLevelMenuRepeater.Visible = false;
                }
                else
                {
                    ThirdLevelMenuRepeater.DataSource = ThirdLevelMenuItems;
                    ThirdLevelMenuRepeater.DataBind();
                }
            }
        }
        public string SecondLevelMenuItem
        {
            get
            {
                return _SecondLevelMenuItem;
            }
            set
            {
                _SecondLevelMenuItem = value;
            }
        }
        private void GetSubMenuItems(string Title, List<MenuItem> MenuItems)
        {
            //root
            SiteMapNode root = SiteMap.RootNode;

            if (SiteMap.RootNode.HasChildNodes)
            {
                IEnumerator rootNodesChildrenEnumerator = SiteMap.RootNode.ChildNodes.GetEnumerator();
                while (rootNodesChildrenEnumerator.MoveNext())
                {
                    SiteMapNode current = (SiteMapNode)rootNodesChildrenEnumerator.Current;

                    //Second level menu items...
                    if (((SiteMapNode)rootNodesChildrenEnumerator.Current).HasChildNodes)
                    {
                        IEnumerator childNodeChildrenEnumerator = ((SiteMapNode)(rootNodesChildrenEnumerator.Current)).ChildNodes.GetEnumerator();
                        while (childNodeChildrenEnumerator.MoveNext())
                        {
                            SiteMapNode childCurrent = (SiteMapNode)childNodeChildrenEnumerator.Current;

                            if (childCurrent.ToString() == SecondLevelMenuItem) //This is what we need now, for example "Risk Tracker"
                            {
                                //Third level menu items...
                                if (((SiteMapNode)childNodeChildrenEnumerator.Current).HasChildNodes)
                                {
                                    IEnumerator ThirdLevelchildNodeChildrenEnumerator = ((SiteMapNode)(childNodeChildrenEnumerator.Current)).ChildNodes.GetEnumerator();
                                    while (ThirdLevelchildNodeChildrenEnumerator.MoveNext())
                                    {
                                        SiteMapNode ThirdLevelchildCurrent = (SiteMapNode)ThirdLevelchildNodeChildrenEnumerator.Current;

                                        if (ThirdLevelchildCurrent.ToString() == Title)
                                        {
                                            GetMenuItems(ThirdLevelchildCurrent, MenuItems); //need menu items here...
                                            return;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }
        private SiteMapNode GetRootNode(string strSiteMapNode)
        {
            //root
            SiteMapNode root = SiteMap.RootNode;
            return root;
        }
        private SiteMapNode GetChildNode(SiteMapNode parent)
        {
            if (parent.HasChildNodes)
            {
                IEnumerator ChildrenEnumerator = parent.ChildNodes.GetEnumerator();
                while (ChildrenEnumerator.MoveNext())
                {
                    return (SiteMapNode)ChildrenEnumerator.Current;
                }
            }
            return null;
        }

        private void GetMenuItems(SiteMapNode Node, List<MenuItem> MenuItems)
        {
            if (Node.HasChildNodes)
            {
                IEnumerator ChildrenEnumerator = Node.ChildNodes.GetEnumerator();
                while (ChildrenEnumerator.MoveNext())
                {
                    SiteMapNode current = (SiteMapNode)ChildrenEnumerator.Current;

                    bool blnAllow = false;
                    foreach (string roleName in current.Roles)
                    {
                        if (roleName.Equals("*"))
                        {
                            blnAllow = true;
                            break;
                        }
                        else if (this.Page.User.IsInRole(roleName))
                        {
                            blnAllow = true;
                            break;
                        }
                    }
                    if (blnAllow)
                    {
                        string Title = current.ToString(); //Title
                        string Desc = current.Description; //Desc
                        string Url = current.Url; //Url

                        MenuItem mi = new MenuItem(Title, Desc, Url);
                        MenuItems.Add(mi);
                    }

                    ////sub menu items...
                    //if (((SiteMapNode)ChildrenEnumerator.Current).HasChildNodes)
                    //{
                    //    //Has children...
                    //}
                }
            }
        }

        protected void ThirdLevelMenuRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null)
            {
                string Title = DataBinder.Eval(e.Item.DataItem, "Title").ToString().Trim();

                FourthLevelMenuItems.Clear();
                GetSubMenuItems(Title, FourthLevelMenuItems);

                Repeater FourthLevelMenuRepeater = (Repeater)e.Item.FindControl("FourthLevelMenuRepeater");
  
                FourthLevelMenuRepeater.DataSource = FourthLevelMenuItems;
                FourthLevelMenuRepeater.DataBind();
            }
            
        }

    }
}